﻿using System;
using System.Linq;
using GdHotel.Data.Interfaces;
using UniqueTechnologies.Data.Interfaces;

namespace GdHotel.Data.Repositories
{
    public class LanguagesRepository : ILanguagesRepository
    {
        private GdHotelEntities entities = new GdHotelEntities();

        public IQueryable<Language> FindAll()
        {
            return entities.Languages.AsQueryable();
        }

        public IQueryable<Language> FindAllByLang(Language lang)
        {
            return entities.Languages.Where(l => l.Id == lang.Id);
        }

        public Language GetById(Int32 id)
        {
            return entities.Languages.SingleOrDefault(l => l.Id == id);
        }

        public void Insert(Language l)
        {
            entities.AddToLanguages(l);
        }

        public void Delete(Language l)
        {
            entities.Languages.DeleteObject(l);
        }

        public void Save()
        {
            entities.SaveChanges();
        }

        public Language GetByShortName(String shortName)
        {
            return entities.Languages.Where(l => l.ShortName.Equals(shortName)).FirstOrDefault();
        }

        public Language GetDefault()
        {
            return 
                entities.Languages.Where(l => l.Default).FirstOrDefault() ?? 
                entities.Languages.FirstOrDefault();
        }

        public void Dispose()
        {
            entities.Dispose();
        }
    }
}
